package mk.edu.ii.navigatemacedonia;


import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class SiteInfoActivity extends Activity {
		private Intent intent;	
		private Site site;
		private Drawable image;
		private String image_url="http://cache.virtualtourist.com/1220585-Skopsko_KaleKale_Fortress-Skopje.jpg";
		// Initialize a handler on the main thread.
	    private Handler handler = new Handler();
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.siteinfoview);
        intent = getIntent();
        load_siteinfo();
        }
    
    public void load_siteinfo(){
    	site = (Site)intent.getSerializableExtra("site");
    	
    	TextView siteName = (TextView)findViewById(R.id.third_TextView01_1);
    	TextView siteCategory = (TextView)findViewById(R.id.third_TextView02_1);
    	TextView siteRating = (TextView)findViewById(R.id.third_TextView03_1);
    	TextView siteInfo = (TextView)findViewById(R.id.third_siteinfo);
       
        siteName.setText(site.getSiteName());
        siteCategory.setText(site.getSiteCategory());
        siteRating.setText(site.getSiteRating().toString()+"        "+site.getSiteDistance().toString());
        image_url=site.getSiteImage();
        
        imageThread();
        siteInfo.setText(site.getSiteDescription());
        
        final Button button = (Button) findViewById(R.id.third_Map_Button);
		button.setOnClickListener(new Button.OnClickListener() {
		public void onClick(View v){
			Intent mapView = new Intent(v.getContext(),MapPreviewActivity.class);
			mapView.putExtra("site", site);
			mapView.putExtra("longitude", intent.getDoubleExtra("longitude", 0d));
			mapView.putExtra("latitude", intent.getDoubleExtra("latitude", 0d));
			startActivity(mapView);
		}});
        
    
    }
    
    public void load_image(String image_url)
    {
    	 image = ImageOperations(image_url);
    }
    
    private Drawable ImageOperations(String url) {
    	
		try {			
			InputStream is = (InputStream) this.fetch(url);
			Drawable d = Drawable.createFromStream(is, "src");						
			return d;
		} catch (MalformedURLException e) {			
			e.printStackTrace();
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
    		
	}

	public Object fetch(String address) throws MalformedURLException,IOException {
		URL url = new URL(address);
				
		URLConnection connection=url.openConnection();
		connection.setReadTimeout(5000);
				
		Object content = connection.getContent();
		return content;
	}

	
	private void imageThread() {
		Thread thread = new Thread(null, doBackgroundThreadProcessing, "Background");
		thread.start();
	}
	
	private Runnable doBackgroundThreadProcessing = new Runnable() {
		public void run() {
			backgroundThreadProcessing();
		}
	};
	
	// Method which does some processing in the background.
	private void backgroundThreadProcessing() {
		load_image(image_url);
		handler.post(doUpdateGUI);
	}
	// Runnable that executes the update GUI method.
	private Runnable doUpdateGUI = new Runnable() {
		public void run() {
			updateGUI();
		}
	};
	private void updateGUI() {
		ImageView imageView = (ImageView)findViewById(R.id.third_image_frame);
    	if(image!=null){
    		imageView.setImageDrawable(image);}
    	else{
    	   	String msg = "Image can not be loaded!";
        	int duration = Toast.LENGTH_SHORT;
        	Toast toast = Toast.makeText(getApplicationContext(), msg, duration);
        	toast.show();
        };
	}
}